package com.kedeng.yangmuyi.common.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import com.kedeng.yangmuyi.exception.MD5Exception;

/**
 * 
 * @ClassName: MD5Util
 * @Description: Generate password or other message to MD5 code.
 * @author rqian
 * @date Aug 8, 2013 2:04:34 PM
 * 
 */
public class MD5Util {

    public static String hashStrWithMD5(String str) {
        StringBuffer sb = new StringBuffer();
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());

            byte byteData[] = md.digest();
            for (int i = 0; i < byteData.length; i++) {
                sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
            }
        } catch (NoSuchAlgorithmException e) {
            throw new MD5Exception();
        }

        return sb.toString();
    }
}
